package com.sinoyd.itszyw.Fragment;

import android.app.Instrumentation;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import static com.sinoyd.itszyw.Activity.BaseActivity.mToast;

/**
 * @author: Sinoyd  --------->作者
 * @description: --------->描述
 * @projectName: ITSZYW  ---------> 项目名称
 * @date: 2017-03-09 ---------> 创建日期
 * @time: 09:03       ---------> 创建时间
 */
public class BaseFragment  extends Fragment{

    private boolean injected = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        injected = true;
        return  x.view().inject(this,inflater,container);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!injected){
            x.view().inject(this,this.getView());
        }
    }


    /**
     * @param params      请求参数
     * @param method      请求方法
     * @param requestCode 请求代码标识
     * @throws Exception
     */
    public void sendHttpRequest(RequestParams params,
                                HttpMethod method, int requestCode)
            throws Exception {
        final int code = requestCode;


        x.http().request(method, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                httpOnSuccess(result, code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (ex instanceof HttpException) { // 网络错误
                    HttpException httpEx = (HttpException) ex;
                    int responseCode = httpEx.getCode();
                    String responseMsg = httpEx.getMessage();
                    String errorResult = httpEx.getResult();
                    showToast(responseMsg + errorResult + responseCode);
                } else { // 其他错误
                    Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                }


            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

    public void httpOnSuccess(String responseStr, int requestCode) {

    }


    /**
     * 返回键
     */
    public void goBack() {
        new Thread() {
            public void run() {
                try {
                    Instrumentation inst = new Instrumentation();
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                } catch (Exception e) {
                    Log.e("sendKeyDownUpSync error", e.toString());
                }
            }
        }.start();
    }


    /**
     * 吐司
     */
    public void showToast(String msg) {

        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);

        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

}
